package com.sqx.modules.search.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sqx.common.utils.Result;
import com.sqx.modules.search.dao.SearchRecordDao;
import com.sqx.modules.search.entity.SearchRecord;
import com.sqx.modules.search.service.SearchRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 搜索记录(SearchRecord)表服务实现类
 *
 * @author makejava
 * @since 2024-05-28 09:33:02
 */
@Service("searchRecordService")
@Slf4j
public class SearchRecordServiceImpl extends ServiceImpl<SearchRecordDao, SearchRecord> implements SearchRecordService {
    @Resource
    private SearchRecordDao searchRecordDao;

    private ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(true);

    @Override
    public void saveRecordInfo(String content, Long userId) {
        reentrantReadWriteLock.writeLock().lock();
        try {
            if (StringUtils.isEmpty(content)) {
                return;
            }
            SearchRecord searchContent = searchRecordDao.selectOne(new QueryWrapper<SearchRecord>().eq("search_content", content).eq("user_id", userId));
            if (ObjectUtil.isEmpty(searchContent)) {
                searchContent = new SearchRecord();
                searchContent.setId(IdUtil.fastSimpleUUID());
                searchContent.setUserId(userId);
                searchContent.setSearchContent(content);
                searchContent.setSearchCount(1);
                searchContent.setSearchTime(LocalDateTime.now());
                searchContent.setCreateTime(LocalDateTime.now());
                searchRecordDao.insert(searchContent);
            } else {
                Integer searchCount = searchContent.getSearchCount();
                searchCount++;
                searchContent.setSearchCount(searchCount);
                searchRecordDao.updateById(searchContent);
            }
        } catch (Exception e) {
            log.error("搜索记录保存修改错误：" + e.getMessage(), e);
        } finally {
            reentrantReadWriteLock.writeLock().unlock();
        }
    }


    @Override
    public Result queryRcordPage(JSONObject params) {
        Integer pageNum = params.getInteger("pageNum");
        Integer pageSize = params.getInteger("pageSize");
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> maps = searchRecordDao.searhCountGroup(params.getString("keys"));
        PageInfo<Map<String, Object>> mapPageInfo = new PageInfo<>(maps);
        return Result.success().put("data", mapPageInfo);
    }

    @Override
    public Result querySearchRecord(JSONObject params) {
        Integer pageNum = params.getInteger("pageNum");
        Integer pageSize = params.getInteger("pageSize");
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String, Object>> maps = searchRecordDao.querySearchRecord(params.getString("content"),
                params.getString("userName"), params.getLong("userId"),
                params.getString("phone"), params.getString("startTime"), params.getString("endTime"));
        PageInfo<Map<String, Object>> mapPageInfo = new PageInfo<>(maps);
        return Result.success().put("data", mapPageInfo);
    }
}
